package demo;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * 使用function解决多分支
 *
 * @author dingwen
 * 2021.06.04 10:58
 */
public class FunctionDemo2 {
    private final Map<String, Function<String, String>> map = new HashMap<>();

    public String append(String appendType) {
        init();
        Function<String, String> function = map.get(appendType);
        return function.apply(appendType);
    }

    public void init() {
        map.put("1", val -> {
            System.out.println("实行了分支1");
            return "1" + "分支1";
        });

        map.put("2", val -> {
            System.out.println("执行了分支2");
            return "2" + "分支2";
        });

        map.put("3", val -> {
            System.out.println("执行分支3");
            return "3" + "分支3";
        });

        map.put("4", val -> {
            System.out.println("执行分支4");
            return "4" + "分支4";
        });
    }

    public static void main(String[] args) {
        FunctionDemo2 functionDemo2 = new FunctionDemo2();
        functionDemo2.init();
        System.out.println(functionDemo2.append("2"));
        System.out.println(functionDemo2.append("1"));
    }
}
